package top.infopub.mgr.fia.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.fia.InvoiceManageDto;
import top.infopub.enums.fia.FiaProcessDetailTypeEnum;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.fia.dao.CommonDao;
import top.infopub.mgr.fia.dao.InvoiceManageDao;
import top.infopub.mgr.fia.domain.InvoiceManageDomain;
import top.infopub.mgr.fia.jsonvo.InvoiceManageJsonVo;
import top.infopub.mgr.fia.service.FiaProcessManageService;
import top.infopub.mgr.fia.service.InvoiceManageService;
import top.infopub.mgr.fia.vo.InvoiceManageVo;
import top.infopub.mgr.flow.service.OAFlowService;
import top.infopub.mgr.hr.dao.StaffInfoDao;
import top.infopub.model.common.ProInfo;
import top.infopub.model.common.HrOrgInfo;
import top.infopub.model.fia.InvoiceManage;
import top.infopub.model.hr.StaffInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import java.util.Date;
import java.util.List;

@Service
public class InvoiceManageServiceImpl extends GenericServiceImpl<InvoiceManage, String> implements
		InvoiceManageService {
	@Autowired
	private InvoiceManageDao invoiceManageDao;

	@Autowired
	private CommonDao commonDao;

	@Autowired
	private StaffInfoDao staffInfoDao;

	@Autowired
	private OAFlowService oaFlowService;

	@Autowired
	private FiaProcessManageService fiaProcessManageService;


	@Override
	public GenericDao<InvoiceManage, String> getDao() {
		return invoiceManageDao;
	}

    @Override
    public Page<InvoiceManageDomain> selectPageByDomainWhere(Page<InvoiceManageDomain> page,
															 InvoiceManageVo invoiceManageVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<InvoiceManageDomain>) invoiceManageDao.selectByDomainWhere(invoiceManageVo);
    }

	@Override
	public InvoiceManageJsonVo queryDetailById(String id) {
		InvoiceManageJsonVo invManJson = new InvoiceManageJsonVo();
		if (StringUtils.isNotBlank(id)) {
			//基础信息
			InvoiceManageDto invManDto = invoiceManageDao.queryDetailById(id);
			invManJson.setInfo(invManDto);
		} else {
			//基础信息
			InvoiceManageDto invManDto = new InvoiceManageDto();
			AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
			StaffInfo staffInfo = staffInfoDao.selectByPrimaryKey(vo.getHrStaffId());
			if (staffInfo == null) {
				invManDto.setStaffName(vo.getRealName());
			} else {
				invManDto.setStaffName(staffInfo.getStaffName());
			}
			invManJson.setInfo(invManDto);

			//项目信息列表
			List<ProInfo> proInfoList = commonDao.queryCapSetupProList(ProProcesssStatusEnum.PRO_STATUS9.getCode());
			invManJson.setProInfoList(proInfoList);
		}
		//组织架构单位列表
		List<HrOrgInfo> orgInfoList = commonDao.queryHrOrgInfoList(null);
		invManJson.setOrgInfoList(orgInfoList);
		return invManJson;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public RspVo edit(InvoiceManageDto invManDto, Integer submitType, AuthorizationVO vo) {
		InvoiceManage tmpIm = new InvoiceManage();
		InvoiceManage invoiceManage = new InvoiceManage();
		BeanUtils.copyProperties(invManDto, invoiceManage);
		if (StringUtils.isNotBlank(invManDto.getId())) {
			//校验资金上交数据是否存在
			tmpIm = invoiceManageDao.selectByPrimaryKey(invManDto.getId());
			if (tmpIm == null) return RspVo.getFailureResponse(400, "融资付息数据不存在");
			//判断审批状态
			if (StringUtils.isNotBlank(tmpIm.getProcessStatus())
					&& (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), tmpIm.getProcessStatus())
					|| StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), tmpIm.getProcessStatus()))) {
				return RspVo.getFailureResponse(400, "审批状态不正确，操作失败");
			}
			invoiceManage.setUptBy(vo.getId());
			invoiceManage.setUptTime(new Date());
			invoiceManageDao.updateByIdSelective(invoiceManage);
		} else {
			invoiceManage.setCodes(IdFactory.getUUIDUpper());
			invoiceManage.setAddBy(vo.getId());
			invoiceManage.setAddTime(new Date());
			invoiceManage.setUptBy(vo.getId());
			invoiceManage.setUptTime(new Date());
			invoiceManageDao.insertSelective(invoiceManage);
		}

		if (submitType == 1) {
			if (org.apache.commons.lang.StringUtils.isNotBlank(tmpIm.getOaId())) {
				oaFlowService.finishOA(tmpIm.getOaId());
			}
			InvoiceManageJsonVo invManJson = queryDetailById(invoiceManage.getId());
			invManJson.setProInfoList(null);
			invManJson.setOrgInfoList(null);
			String detailJson = JSON.toJSONString(invManJson);
			fiaProcessManageService.sendProcess(FiaProcessDetailTypeEnum.PRO_NODE_TYPE8, detailJson, invoiceManage.getId(), invoiceManage.getProCode(), vo);
		}
		return RspVo.getSuccessResponse();
	}
}